<?php
namespace app\ausadmin\controller;

use think\Request;
use think\Db;
use think\Session;
use think\Cookie;
use think\Controller;
use think\Config;
use Godok\Org\Adminauth;
/**
 * Login Controller
 */
class Login extends Controller
{
	/**
	 * 首页
	 */
	public function index(){
	    if ( Request::instance()->isPost() ) {
	    	//var_dump(input('post.'));die;
	        // 做一个简单的登录 组合where数组条件
	        $username = Request::instance()->post('username', '', 'Godok\Org\Filter::username');
	        if( empty( $username ) ) {
	            return ['code'=>-1001,'msg'=>lang('用户名格式错误！')];
	        }
	        $password =Request::instance()->post('password', '', 'Godok\Org\Filter::password');
	        if( empty( $password ) ) {
	            return ['code'=>-1002,'msg'=>lang('密码格式错误！')];
	        }
	        $password = md123( $password );

	        //Cookie::set('username', $username, 3600*24*7);
	        $data=Db::name( Config::get('auth.admin_user') )->where(['username' => $username,'deleted'=>0])->find();

	        if (empty($data)) {
	            return ['code'=>-1000,'msg'=>lang('用户名错误')];
	        } elseif ($data['password'] != $password) {
	            return ['code'=>-1010,'msg'=>lang('密码错误')];
	        } elseif ( 0 == $data['status']) {
	            return ['code'=>-1020,'msg'=>lang('帐号被禁用')];
	        } elseif ( 2 == $data['status']) {
	            return ['code'=>-1030,'msg'=>lang('帐号审核中')];
	        } else {
	            $user=[
	                'id'=>$data['id'],
	                'username'=>$data['username'],
	                'nickname'=>$data['nickname'],
	                'avatar'=>$data['avatar'],
	                'phone'=>$data['phone'],
	                'rules'=>[],
	                'groupids'=>[]
	            ];
	            if($data['avatar']){
	            	$data['avatar'] = Config::get('resourceurl').$data['avatar'];
	            }else{
	            	$data['avatar'] = Config::get('site.resource_url').'images/a1.png';
	            }
	            //Session::set('avatar', Config::get('resourceurl').$data['avatar'] ?: Config::get('site.resource_url').'images/a1.png');
	            //Cookie::set('nickname', $data['nickname'], 3600*24*7);
                //Session::set('oid',1);//oid 总管理后台无需oid
                Session::set('avatar',$data['avatar']);
	            Session::set('adminnickname',$data['nickname']);//用户昵称
	            Session::set('adminusername',$data['username']);
	            Session::set('adminid',$data['id']);//用户ID

	            //获取权限组信息
	            $groups = Adminauth::getGroups( $data['id'] );
	            $group23 = Db::name( Config::get('auth.admin_group') )->field('id,rules')->where('id IN(2,3) AND status=1')->select();
	            $groups = array_merge($groups,$group23);
	            if (empty($groups)) {
	                return ['code'=>-1040,'msg'=>lang('帐号无权限')];
	            } else {
	                $rules = '';
	                foreach ( $groups as $group) {
	                    $rules .= $rules ? ',' . $group['rules'] : $group['rules'];
	                    if ($group['id'] !=2 && $group['id'] != 3) {
	                        $user['groupids'][] = $group['id'];
	                    }
	                    
	                }
	                if ( !empty($rules) ) {
	                    $user['rules'] = array_unique( explode(',', $rules) );
	                }
	                
	                $user['_freshtime'] = time();
	            }
	            //判断用户组是否被禁用
	            Adminauth::clear();
	            Adminauth::user($user);
	            //保存登录信息
	            Db::name( Config::get('auth.admin_user') )
	            ->where(['id' => $data['id']])
	            ->update([
	            'last_login_ip' => Request::instance()->ip(),
	            'last_login_time' => time()
	            ]);
	            $oid = 0;$m = 'ausadmin'; $c = 'Login'; $action="index"; $uid=session('adminid');; $querystring = '登录';
                $ip = get_ip();$os = get_os();
                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
	            return ['code'=>0,'msg'=>lang('登录成功'),'url'=>\think\Request::instance()->root().'/ausadmin/Index/index'];
	        }
	    } else {
	        return $this->fetch();
	    }
        
	}
    /**
     * 退出
     */
    
    public function logout()
    {
    	if(session('adminid')){
    		$uids = session('adminid');
    	}
    	
        Adminauth::clear();

       	session(null);
       	cookie(null);
       	if(isset($uids)){
			$oid = 0;$m = 'ausadmin'; $c = 'Login'; $action="logout"; $uid=$uids; $querystring = '注销登录';
            $ip = get_ip();$os = get_os();
            set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
       	}
       	
       $this->redirect('index');
    }

    /**
     * 忘记密码
     */
    public function forget()
    {
        return $this->fetch();
    }



}

